package com.guigu.qxl.controller;

import com.github.pagehelper.PageInfo;
import com.guigu.qxl.pojo.*;
import com.guigu.qxl.service.MDesignProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("MDesignProcedure")
public class MDesignProcedureController {
    @Autowired
    MDesignProcedureService mdesignProcedureService;
    @RequestMapping("showAllMdesign")
    public PageVo<QMDesignProcedureDetails> showAllMdesign(Integer pageNum, Integer pageSize){
        PageInfo<QMDesignProcedureDetails> pageInfo=mdesignProcedureService.showAllMdesign(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @RequestMapping("MDesignProcedureRegister")
    public Map MDesignProcedureRegister(@RequestBody MDesignProcedureVo mDesignProcedureVo){
        Map map=new HashMap();
        map.put("code","-1");
        map.put("msg","登记失败");
        String [] arg1=mDesignProcedureVo.getSelect1().split("/");
        mDesignProcedureVo.setFirstKindId(arg1[0]);
        mDesignProcedureVo.setFirstKindName(arg1[1]);
        String [] arg2=mDesignProcedureVo.getSelect2().split("/");
        mDesignProcedureVo.setSecondKindId(arg2[0]);
        mDesignProcedureVo.setSecondKindName(arg2[1]);
        String [] arg3=mDesignProcedureVo.getSelect3().split("/");
        mDesignProcedureVo.setThirdKindId(arg3[0]);
        mDesignProcedureVo.setThirdKindName(arg3[1]);
        int num=0;
        for (QMDesignProcedureDetails d: mDesignProcedureVo.getList()) {
            num=num+d.getSubtotal();
        }
        mDesignProcedureVo.setCostPriceSum(num);
        if(mdesignProcedureService.MDesignProcedureRegister(mDesignProcedureVo)){
            map.put("code","1");
            map.put("msg","登记成功");
        }
        return map;
    }
    @RequestMapping("updMDesignProcedure")
    public Map updMDesignProcedure(@RequestBody QMDesignProcedure mdesignProcedure){
        Map map=new HashMap();
        map.put("code","-1");
        map.put("msg","登记失败");
        if(mdesignProcedureService.UpdMDesignProcedure(mdesignProcedure)){
            map.put("code","1");
            map.put("msg","登记成功");
        }
        return map;
    }
    @RequestMapping("mDesignProcedureModuleRegister")
    public Map mDesignProcedureModuleRegister(
            @RequestBody MDesignProcedureModuleVo mdesignProcedureModuleVo){
        Map map=new HashMap();
        map.put("code","-1");
        map.put("msg","登记失败");
        if(mdesignProcedureService.mDesignProcedureModuleRegister(mdesignProcedureModuleVo)){
            map.put("code","1");
            map.put("msg","登记成功");
        }
        return map;
    }
    @RequestMapping("removeById")
    public void removeById(@RequestParam("productId") String productId,
                           @RequestParam("id") int id){
        mdesignProcedureService.removeById(productId,id);
    }
    @RequestMapping("showMDesignProcedureById")
    public QMDesignProcedure showMDesignProcedureById(QMDesignProcedure mdesignProcedure){
        return mdesignProcedureService.showMDesignProcedureById(mdesignProcedure);
    }
    @RequestMapping("showMDesignProcedureDetailsById")
    public QMDesignProcedureDetails showMDesignProcedureDetailsById(QMDesignProcedureDetails mdesignProcedureDetails){
        return mdesignProcedureService.showMDesignProcedureDetailsById(mdesignProcedureDetails);
    }
    @RequestMapping("showMDesignProcedureDetailsByParentId")
    public List<QMDesignProcedureDetails> showMDesignProcedureDetailsByParentId(QMDesignProcedure mdesignProcedure){
        return mdesignProcedureService.showMDesignProcedureDetailsByParentId(mdesignProcedure);
    }
    @RequestMapping("mdesignProcedureChange")
    public Map mdesignProcedureChange(@RequestBody MDesignProcedureVo mdesignProcedureVo){
        Map map=new HashMap();
        map.put("code","-1");
        map.put("msg","修改失败");
        if(mdesignProcedureService.mdesignProcedureChange(mdesignProcedureVo)){
            map.put("code","1");
            map.put("msg","修改成功");
        }
        return map;
    }
    @RequestMapping("showProuctByProductId")
    public List<QDModuleDetails> showProuctByProductId(String productId){
        return mdesignProcedureService.showProuctById(productId);
    }
    @RequestMapping("showProcedureModuleByParentId")
    public List<QMDesignProcedureModule> showProcedureModuleByParentId(int id){
        return mdesignProcedureService.showProcedureModuleByParentId(id);
    }
    @RequestMapping("DesignModuleCheckOk")
    public Map DesignModuleCheckOk(@RequestBody QMDesignProcedure mdesignProcedure){
        Map map=new HashMap();
        String productId=mdesignProcedure.getProductId();
        QDFile dfile=mdesignProcedureService.showDFileByProductId(productId);
        int sum=mdesignProcedure.getCostPriceSum()+mdesignProcedure.getModuleCostPriceSum()-dfile.getRealCostPrice();
        if(sum>0){
            mdesignProcedure.setCheckTag("S001-2");
            map.put("code","-1");
            map.put("msg","审核不通过，当前已超出实际成本"+sum+"元,推荐重新设计");
        }else{
            mdesignProcedure.setCheckTag("S001-1");
            map.put("code","1");
            map.put("msg","上传成功");
        }
        mdesignProcedureService.DesignModuleCheckOk(mdesignProcedure);
        return map;
    }
    @RequestMapping("mdesignProcedureCheckOk")
    public Map mdesignProcedureCheckOk(@RequestBody QMDesignProcedure mdesignProcedure){
        System.out.println(mdesignProcedure);
        Map map=new HashMap();
        map.put("code","-1");
        map.put("msg","上传失败");
        if(mdesignProcedureService.mdesignProcedureCheckOk(mdesignProcedure)){
            map.put("code","1");
            map.put("msg","上传成功");
        }
        return map;
    }
    @GetMapping("pageS0010MDesignProcedure")
    public PageVo<QMDesignProcedure> pageS0010MDesignProcedure(Integer pageNum, Integer pageSize){
        PageInfo<QMDesignProcedure> pageInfo=mdesignProcedureService.pageS0010MDesignProcedure(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @GetMapping("pageS0011MDesignProcedure")
    public PageVo<QMDesignProcedure> pageS0011MDesignProcedure(Integer pageNum, Integer pageSize){
        PageInfo<QMDesignProcedure> pageInfo=mdesignProcedureService.pageS0011MDesignProcedure(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @GetMapping("pageS0011G0020MDesignProcedure")
    public PageVo<QMDesignProcedure> pageS0011G0020MDesignProcedure(Integer pageNum, Integer pageSize){
        PageInfo<QMDesignProcedure> pageInfo=mdesignProcedureService.pageS0011G0020MDesignProcedure(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @GetMapping("pageS0011G0022MDesignProcedure")
    public PageVo<QMDesignProcedure> pageS0011G0022MDesignProcedure(Integer pageNum, Integer pageSize){
        PageInfo<QMDesignProcedure> pageInfo=mdesignProcedureService.pageS0011G0022MDesignProcedure(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @GetMapping("pageG0020MDesignProcedure")
    public PageVo<QMDesignProcedure> pageG0020MDesignProcedure(Integer pageNum, Integer pageSize){
        PageInfo<QMDesignProcedure> pageInfo=mdesignProcedureService.pageG0020MDesignProcedure(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @GetMapping("pageS0011G0021MDesignProcedure")
    public PageVo<QMDesignProcedure> pageS0011G0021MDesignProcedure(Integer pageNum, Integer pageSize){
        PageInfo<QMDesignProcedure> pageInfo=mdesignProcedureService.pageS0011G0021MDesignProcedure(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }
    @GetMapping("pageMDesignProcedure")
    public PageVo<QMDesignProcedure> pageMDesignProcedure(Integer pageNum, Integer pageSize){
        PageInfo<QMDesignProcedure> pageInfo=mdesignProcedureService.pageMDesignProcedure(pageNum,pageSize);
        PageVo pp=new PageVo();
        pp.setTotal(Integer.parseInt(pageInfo.getTotal()+""));
        pp.setRows(pageInfo.getList());
        return pp;
    }

}
